package com.hee.erp.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hee.erp.modules.entity.SysUserContract;
import com.hee.erp.modules.entity.SysUserHealthReport;
import com.hee.erp.modules.mapper.SysUserContractMapper;
import com.hee.erp.modules.service.ISysUserContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hee.erp.vo.UserContractListProperty;
import com.hee.erp.vo.UserHealthReportListProperty;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author weizehong
 * @since 2022-03-06
 */
@Service
@Transactional
public class SysUserContractServiceImpl extends ServiceImpl<SysUserContractMapper, SysUserContract> implements ISysUserContractService {

    @Override
    public List<SysUserContract> listByUserId(String userId) {

        LambdaQueryWrapper<SysUserContract> qw = new LambdaQueryWrapper<>();
        qw.eq(SysUserContract::getUserId, userId);
        return this.list(qw);
    }

    @Override
    public List<SysUserContract> saveUserContracts(List<SysUserContract> list) {

        for (SysUserContract sysUserContract : list) {
            if (sysUserContract.getId().contains("-")) {
                //新增
                sysUserContract.setId(null);
                this.save(sysUserContract);
            } else {
                //修改
                this.updateById(sysUserContract);
            }
        }

        return list;
    }

    @Override
    public List<UserContractListProperty> getUserContractProperties(String userId) {
        List<SysUserContract> contractList = this.listByUserId(userId);

        return convertToProperty(contractList);
    }

    @Override
    public List<UserContractListProperty> convertToProperty(List<SysUserContract> contractList) {

        List<UserContractListProperty> list = new ArrayList<>();
        HashSet<String> yearSet = contractList.stream().map(contract -> contract.getYear()).collect(Collectors.toCollection(HashSet::new));
        for (String year : yearSet) {
            List<SysUserContract> yearList = contractList.stream().filter(contract -> contract.getYear().equals(year)).collect(Collectors.toList());

            UserContractListProperty contractProperties = new UserContractListProperty();
            contractProperties.setYear(year);
            contractProperties.setList(yearList);

            list.add(contractProperties);
        }

        List<UserContractListProperty> resultList = list.stream().sorted(Comparator.comparing(UserContractListProperty::getYear).reversed()).collect(Collectors.toList());

        if(resultList.size() == 0) {
            UserContractListProperty userContractProperty = new UserContractListProperty();
            userContractProperty.setYear(LocalDate.now().getYear() + "年");
            userContractProperty.setList(new ArrayList<>());

            resultList.add(userContractProperty);
        }
        return resultList;
    }

    @Override
    public List<UserContractListProperty> saveUserContractPropertyList(List<UserContractListProperty> list) {
        List<SysUserContract> contractList = new ArrayList<>();
        for (UserContractListProperty userContractProperty : list) {
            List<SysUserContract> contractList1 = userContractProperty.getList();

            contractList.addAll(contractList1);
        }
        contractList = saveUserContracts(contractList);
        List<UserContractListProperty> resultList = convertToProperty(contractList);
        return resultList;
    }



}
